package popple.core.renders
{
	import com.adobe.utils.AGALMiniAssembler;
	import com.adobe.utils.PerspectiveMatrix3D;
	
	import flash.display3D.Context3D;
	import flash.display3D.Context3DBlendFactor;
	import flash.display3D.Context3DCompareMode;
	import flash.display3D.Context3DProgramType;
	import flash.display3D.Context3DTriangleFace;
	import flash.display3D.IndexBuffer3D;
	import flash.display3D.Program3D;
	import flash.display3D.VertexBuffer3D;
	import flash.display3D.textures.Texture;
	import flash.events.EventDispatcher;
	import flash.geom.Matrix3D;
	
	public class BaseRender extends EventDispatcher
	{
		public var context:Context3D;
		protected var mWidth:uint;
		protected var mHeight:uint;
		
		protected var mEnableDepthAndStencil:Boolean;
		protected var mProgram:Program3D;
		//protected var mProjection:PerspectiveMatrix3D;
		protected var mNear:Number=.001;
		protected var mFar:Number=1000;
		protected var mAgalVertex:AGALMiniAssembler;
		protected var mFragment:AGALMiniAssembler;
		protected var mVertexSource:String;
		protected var mFragmentSource:String;
		
		
		
		public function BaseRender(context:Context3D)
		{
			
			mAgalVertex=new AGALMiniAssembler;
			mFragment=new AGALMiniAssembler;
			//mProjection=new PerspectiveMatrix3D;
			
			this.context=context;
			mProgram=context.createProgram();
			buildProgram();
		}
	
		private function buildProgram():void
		{
			
			setAGALCode();
			mAgalVertex.assemble(Context3DProgramType.VERTEX, mVertexSource);
			mFragment.assemble(Context3DProgramType.FRAGMENT, mFragmentSource);
			mProgram.upload(mAgalVertex.agalcode,mFragment.agalcode);
			context.setProgram(mProgram);
			//context.setProgramConstantsFromMatrix(Context3DProgramType.VERTEX,8,mProjection,true);
		}
		public function setVerBufferAt(index:uint,buffer:VertexBuffer3D,startIndex:uint,len:uint):void
		{
			
		}
		public function clear():void{context.clear()}
		public function present():void{context.present()}
		protected function setAGALCode():void
		{
			
		}
		

		
		public function configureBackBuffer(width:uint,height:uint,antiAlias:int=8,enableDepthAndStencil:Boolean=true):void
		{
			mEnableDepthAndStencil=enableDepthAndStencil;
			context.enableErrorChecking=true;
			context.configureBackBuffer(width,height,antiAlias,mEnableDepthAndStencil);
			context.setDepthTest(true, Context3DCompareMode.ALWAYS);
			context.setCulling(Context3DTriangleFace.NONE);
			context.setBlendFactors(Context3DBlendFactor.SOURCE_ALPHA,Context3DBlendFactor.ONE_MINUS_SOURCE_ALPHA);
			//mProjection.perspectiveFieldOfViewLH(1,mWidth/mHeight,mNear,mFar);
		}
	}
}
